<template>
  <div class="app-container">
    <el-row>
      <!-- 搜索区域 -->
      <el-card style="margin:0 0 5px 5px" v-show="showSearch">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item label="模板名称" prop="Q_table_S_LK">
            <el-input v-model="queryData.Q_table_S_LK" placeholder="模板名称" />
          </el-form-item>
          <el-form-item>
            <el-button class="filter-item m-l-10" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item m-l-10" type="info" round icon="el-icon-refresh" @click="handleClear">
              清空
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <el-col :span="1.5">
            <el-input v-model="template" size="mini" plan placeholder="创建模板名称" />
          </el-col>
          <el-col :span="1.5">
            <el-upload
              v-if="template"
              class="upload-demo inline-block"
              :headers="headers"
              with-credentials
              :limit="1"
              :action="uploadUrl"
              :show-file-list="false"
              :data="uploadFileData(template)"
              :on-success="uploadFileSuccess"
            >
              <el-button type="primary" size="mini" plan icon="el-icon-upload2">上传</el-button>
            </el-upload>
          </el-col>
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" />
        </el-row>
        <el-table
          ref="file_table"
          v-loading="tableLoading"
          row-key="fileId"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
          @sort-change="sortChange"
        >
          <el-table-column align="center" label="模板名称" prop="table" width="300" show-overflow-tooltip />
          <el-table-column align="center" label="文件名称" prop="originalFilename" show-overflow-tooltip />
          <el-table-column align="center" label="扩展名" prop="contentType" sortable width="150" show-overflow-tooltip />
          <el-table-column align="center" label="操作" width="180" fixed="right">
            <template slot-scope="scope">
              <el-link v-permission="['file:template:export']" class="m-r-10" type="primary" :href="downloadUrl(scope.row.fileId)" icon="el-icon-download">下载</el-link>
              <el-link v-permission="['file:template:delete']" class="m-r-10" type="danger" icon="el-icon-delete" @click.native="deleteFile(scope.row.fileId)">删除</el-link>
              <el-dropdown>
                <span class="el-dropdown-link" style="font-size: smaller">
                  <i class="el-icon-arrow-down el-icon--right" />更多
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-permission="['file:template:edit']">
                    <el-upload
                      class="upload-demo"
                      :headers="headers"
                      with-credentials
                      :limit="1"
                      :action="uploadUrl"
                      :show-file-list="false"
                      :data="updateFileData(scope.row.fileId, scope.row.table)"
                      :on-success="uploadFileSuccess"
                    >
                      <el-link type="success" :underline="false" icon="el-icon-upload2">更新模板</el-link>
                    </el-upload>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData" />
      </el-card>
    </el-row>
  </div>
</template>

<script>
import { fileManageTable, deleteFile } from '@/api/tool/file'
import { getAuthenticationHeader } from '@/utils/store'

export default {
  data() {
    return {
      showSearch: true,
      queryData: {
        Q_channel_S_EQ: 5,
        Q_table_S_LK: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      template: undefined,
      headers: getAuthenticationHeader(),
      uploadUrl: this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/api/file/manage/template/upload`
    }
  },
  computed: {
    downloadUrl() {
      return function(value) {
        return encodeURI(this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/api/file/download/${value}`)
      }
    },
    uploadFileData() {
      return function(value) {
        return {
          table: value,
          channel: 5
        }
      }
    },
    updateFileData() {
      return function(fileId, table) {
        return {
          id: fileId,
          table: table,
          channel: 5
        }
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getTableData()
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      fileManageTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.file_table.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    uploadFileSuccess(response) {
      if (response.code === 200) {
        this.template = undefined
        this.$modal.msgSuccess('上传成功')
        this.getTableData()
      } else {
        this.$modal.msgError(response.msg)
      }
    },
    deleteFile(fileId) {
      this.$confirm('是否删除该模板件?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        deleteFile({ key: fileId }).then(response => {
          this.getTableData()
          this.$modal.msgSuccess(response.msg)
        }).catch(e => {})
      }).catch(e => {})
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
.inline-block {
  display: inline-block;
}
</style>
